package com.soft.hwx.scdapp;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import android.support.v7.app.AppCompatActivity;

import java.util.HashMap;
import java.util.Map;

import static com.soft.hwx.scdapp.SDCardHelper.readIp;
import static com.soft.hwx.scdapp.SDCardHelper.saveIp;


public class IpConfig extends AppCompatActivity{

    private EditText mIpAddr;                           //IP地址
    private EditText mPort;                            //端口
    private Button mSureButton;                       //确定按钮
    private Button mCancelButton;                     //取消按钮
    private UserDataManager mUserDataManager;         //用户数据管理类
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ip_config);
        mIpAddr = (EditText) findViewById(R.id.ip_config_addr_tv);
        mPort = (EditText) findViewById(R.id.ipConfigPortTv);

        mSureButton = (Button) findViewById(R.id.ip_config_btn_sure);
        mCancelButton = (Button) findViewById(R.id.ip_config_btn_cancel);

        mSureButton.setOnClickListener(m_register_Listener);      //注册界面两个按钮的监听事件
        mCancelButton.setOnClickListener(m_register_Listener);

        if (mUserDataManager == null) {
            mUserDataManager = new UserDataManager(this);
            mUserDataManager.openDataBase();                              //建立本地数据库
        }
        Context content =getApplicationContext();
        Map<String,String> mMap= new HashMap<String,String>();
        mMap = readIp(content);

        if(mMap == null){
            saveIp(content,"192.168.1.100","8013");
            mIpAddr.setText("192.168.1.100");
            mPort.setText("8013");
        }else{
            mIpAddr.setText(mMap.get("ip_addr"));
            mPort.setText(mMap.get("port"));
        }

    }
    View.OnClickListener m_register_Listener = new View.OnClickListener() {    //不同按钮按下的监听事件选择
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.ip_config_btn_sure:                       //确认按钮的监听事件
                    ip_config_check();
                    break;
                case R.id.ip_config_btn_cancel:                     //取消按钮的监听事件,由注册界面返回登录界面
                    Intent intent_ip_config_to_Login = new Intent(IpConfig.this,Login.class) ;    //切换User Activity至Login Activity
                    startActivity(intent_ip_config_to_Login);
                    finish();
                    break;
            }
        }
    };
    public void ip_config_check() {                                //确认按钮的监听事件

        String ip_addr = mIpAddr.getText().toString().trim();
        String port = mPort.getText().toString().trim();

        //检查

        Context content =getApplicationContext();
        saveIp(content,ip_addr,port);

        Intent intent_Register_to_Login = new Intent(IpConfig.this,Login.class) ;    //切换User Activity至Login Activity
        startActivity(intent_Register_to_Login);
        finish();

        /*
        if (isUserNameAndPwdValid()) {
            String userName = mAccount.getText().toString().trim();
            String userPwd = mPwd.getText().toString().trim();
            String userPwdCheck = mPwdCheck.getText().toString().trim();
            //检查用户是否存在
            int count=mUserDataManager.findUserByName(userName);
            //用户已经存在时返回，给出提示文字
            if(count>0){
                Toast.makeText(this, getString(R.string.name_already_exist, userName),Toast.LENGTH_SHORT).show();
                return ;
            }
            if(userPwd.equals(userPwdCheck)==false){     //两次密码输入不一样
                Toast.makeText(this, getString(R.string.pwd_not_the_same),Toast.LENGTH_SHORT).show();
                return ;
            } else {
                ScdUserData mUser = new ScdUserData(userName, userPwd);
                mUserDataManager.openDataBase();
                long flag = mUserDataManager.insertUserData(mUser); //新建用户信息
                if (flag == -1) {
                    Toast.makeText(this, getString(R.string.register_fail),Toast.LENGTH_SHORT).show();
                }else{
                    Toast.makeText(this, getString(R.string.register_success),Toast.LENGTH_SHORT).show();
                    Intent intent_Register_to_Login = new Intent(Register.this,Login.class) ;    //切换User Activity至Login Activity
                    startActivity(intent_Register_to_Login);
                    finish();
                }
            }
        }*/
    }

}
